package com.xlx.openLink.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xlx.openLink.base.BasePage;
import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.dao.PortMapper;
import com.xlx.openLink.entity.pojo.Alarm;
import com.xlx.openLink.entity.pojo.Device;
import com.xlx.openLink.entity.pojo.Port;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.template.OpenLinkTemplate;
import com.xlx.openLink.uitls.AssertUtils;
import com.xlx.openLink.vo.RequestAlarmList;
import com.xlx.openLink.vo.RequestPortLst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class PortService {

    @Autowired
    private PortMapper portMapper;

    @Autowired
    private OpenLinkTemplate openLinkTemplate;

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public BasePage<Port> listByPage(RequestPortLst request) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Port> list = portMapper.listByPage(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<Port>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }


    /**
     * 批量更新操作
     *
     * @param list
     * @return
     */
    @Transactional
    public int updateBatch(List<Port> list) {
        int loopCount = list.size() / 100;
        int left = list.size() % 100;
        for (int i = 0; i < loopCount; i++) {
            int fromIndex = i * 100;
            portMapper.updateBatch(list.subList(fromIndex, fromIndex + 100));
        }
        if (left > 0) {
            int fromIndex = loopCount * 100;
            portMapper.updateBatch(list.subList(fromIndex, fromIndex + left));
        }
        return 1;
    }

    @Transactional
    public int update(Port port) {
        //TODO
        return portMapper.updateByPrimaryKeySelective(port);
    }

    @Transactional
    public Port selectById(String pkId) {
        //TODO
        return portMapper.selectByPrimaryKey(pkId);
    }

    /**
     * 批量更新操作
     *
     * @param list
     * @return
     */
    @Transactional
    public int insertBatch(List<Port> list) {
        int loopCount = list.size() / 100;
        int left = list.size() % 100;
        for (int i = 0; i < loopCount; i++) {
            int fromIndex = i * 100;
            portMapper.insertBatch(list.subList(fromIndex, fromIndex + 100));
        }
        if (left > 0) {
            int fromIndex = loopCount * 100;
            portMapper.insertBatch(list.subList(fromIndex, fromIndex + left));
        }
        return 1;
    }


}
